package com.sdruixinggroup.mondayb2b.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sdruixinggroup.mondayb2b.API.API;
import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.interfaces.ObjectCallBack;
import com.sdruixinggroup.mondayb2b.models.ModelBeen;
import com.sdruixinggroup.mondayb2b.utils.UserInfoUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import okhttp3.Call;

/**
 * Created by tangjie on 2017/11/11.
 */

public class SexActivity extends BaseActivity {
    private RelativeLayout male, female;
    private ImageView male_check, female_check;
    private ImageView topbar_left;
    private TextView topbar_center;
    private int gender = -1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sex);
        gender = getIntent().getIntExtra("gender",-1);
        topbar_left = (ImageView) findViewById(R.id.topbar_left);
        topbar_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(Activity.RESULT_OK);
                finish();
            }
        });

        topbar_center = (TextView) findViewById(R.id.topbar_title);
        topbar_center.setText("修改性别");
        initView();

    }

    private void initView() {
        male = (RelativeLayout) findViewById(R.id.male);
        female = (RelativeLayout) findViewById(R.id.female);
        male_check = (ImageView) findViewById(R.id.male_checked);
        female_check = (ImageView) findViewById(R.id.female_checked);

        male.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dealView(view);
            }
        });
        female.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dealView(view);
            }
        });

        if(gender == 1){
            male_check.setVisibility(View.VISIBLE);
            female_check.setVisibility(View.INVISIBLE);
        }else if(gender == 2){
            male_check.setVisibility(View.INVISIBLE);
            female_check.setVisibility(View.VISIBLE);
        }else {
            male_check.setVisibility(View.INVISIBLE);
            female_check.setVisibility(View.INVISIBLE);
        }
    }

    private void dealView(View view) {
        if (view == male) {
            male_check.setVisibility(View.VISIBLE);
            female_check.setVisibility(View.INVISIBLE);
            changsex(1);
        }
        if (view == female) {
            male_check.setVisibility(View.INVISIBLE);
            female_check.setVisibility(View.VISIBLE);
            changsex(2);
        }
    }

    private void changsex(int sex){
        String url = API.MEMBER_GENDER+sex+"&access_token=" + UserInfoUtil.getInfoToken(getApplicationContext());
        HashMap<String,String> headers = new HashMap<>();
        headers.put("Accept","*/*");
        TypeToken<ModelBeen> typeToken = new TypeToken<ModelBeen>(){};
        OkHttpUtils.get().url(url).headers(headers).build().execute(new ObjectCallBack<ModelBeen>(typeToken) {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(ModelBeen response, int id) {
                if(null != response){
                    if(response.err_code == 0){
                        showToast("修改成功！");
                    }else {
                        showToast(response.err_msg);
                    }
                }
            }
        });
    }
}
